<template>
  <div class="user">
    <div class="right-con">
      <div>
        <div class="con-tit mb30">充值设置</div>
        <div>
          <el-table
            :data="tableData"
            style="width: 100%">
            <el-table-column
              prop="coin"
              label="金币量">
            </el-table-column>
            <el-table-column
              prop="coin_award"
              label="赠送量（金币）">
            </el-table-column>
            <el-table-column
              prop="money"
              label="对应金额（元）">
            </el-table-column>
            <el-table-column
              label="操作">
              <template scope="props">
                <el-switch
                  v-model="props.row.status"
                  on-text="显示"
                  off-text="隐藏"
                  @change="setdolls(props.row,'is_show')">
                </el-switch>
              </template>
            </el-table-column>
            <el-table-column
              label="置顶">
              <template scope="props">
                  <span @click="setdolls(props.row)" style="cursor: pointer;">
                     <img width="20" src="../../assets/images/top.png" alt="">
                  </span>
              </template>
            </el-table-column>
            <el-table-column
              label="详情设置">
              <template scope="props">
                <el-button
                  class="fs14"
                  type="text"
                  size="small"
                  @click="goeditRecharge(props.row)">
                  编辑
                </el-button>
              </template>
            </el-table-column>
          </el-table>
        </div>
      </div>
    </div><!--.right-con-->
    <div v-if="loading" class="loadingbox"></div>
  </div><!--.user-->
</template>

<script type="text/ecmascript-6">
  import api from '../../assets/js/api';

  export default {
    name: "user",
    data() {
      return {
        pageData: null,
        searchTxt: '',
        entryTxt: '',
        page: 1,
        per_page: 5,
        tableData: null,
        tableList: null,
        is_moreData: false, //是否有更多
        is_preData: false,//是否有上一页
        tableDataLength: null, //总长度
        pages: null, //总页数
        is_change: false,
        loading: false,
        modelData: null,
        dialogTableVisible: false
      }
    },
    mounted: function () {
      this.getlist();
    },
    methods: {
      getlist: function () {
        this.loading = true;
        api.fetch("/api/admin/coins")
          .then(res => {
            for (let i = 0; i < res.data.length; i++) {
              if (res.data[i].stick_time == 0) {
                res.data[i].stick_time = false;
              } else {
                res.data[i].stick_time = true;
              }
              if (res.data[i].status == -1) {
                res.data[i].status = false;
              } else {
                res.data[i].status = true;
              }
            }
            this.tableData = res.data;
            this.loading = false;
          }).catch();
      },
      setdolls: function (obj,type) {
        if(type == 'is_show'){
          let change_ok=true;
          if(obj.status == false){
            let colseArr=0;
            for(let i=0;i<this.tableData.length;i++){
              if(this.tableData[i].status == true){
                colseArr++
              }
            }
            if(colseArr < 2){
              change_ok=false;
              obj.status =true;
              this.$message.error('最少显示两个！');
            }
          }
          if(change_ok == true){
            if (obj.status) {
              obj.statusa = 1;
            } else {
              obj.statusa = -1;
            }
            api.fetch("/api/admin/coins/" + obj.id, {
              method: 'POST',
              data: {
                coin: obj.coin,
                money: obj.money,
                status: obj.statusa,
                _method: "put"
              }
            })
              .then(res => {
                this.$message({
                  message: '设置成功！',
                  type: 'success'
                });
              }).catch();
          }
        }else{
            obj.is_top =1;
            api.fetch("/api/admin/coins/" + obj.id, {
              method: 'POST',
              data: {
                coin: obj.coin,
                money: obj.money,
                is_top: obj.is_top,
                _method: "put"
              }
            })
              .then(res => {
                this.$message({
                  message: '设置成功！',
                  type: 'success'
                });
                  this.getlist()
              }).catch();
        }

      },
      goeditRecharge(obj) {
        if (obj.status) {
          obj.statusa = 1;
        } else {
          obj.statusa = -1;
        }
        if (obj.stick_time) {
          obj.is_top = 1;
        } else {
          obj.is_top = 0;
        }
        this.$router.push('/rechargeBox/editRecharge' +
          '?id=' + obj.id
          + '&coin=' + obj.coin
          + '&coin_award=' + obj.coin_award
          + '&money=' + obj.money
          + '&status=' + obj.statusa
          + '&stick_time=' + obj.is_top);
      }
    }
  }
</script>

<style scoped>
  .user {
    height: 100%;
    min-width: 1200px;
  }
</style>
